<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/11/13 18:00
 * Desc: 组合产品
 */
class Model_Api_V2_Marketing_Strategy_Combination extends Contract_Api_Marketing_Strategy
{
    private static $instance = null;

    /**
     * 支持的组合产品id
     * @var array
     */
    private static $product_ids;

    private $application_name = '组合套餐';

    private $application_code = 'system_combination';

    public function __construct($type_id)
    {

    }

    public static function get_instance(int $type_id, array $params = [])
    {
        // 应用是否安装
        if (!St_Functions::is_normal_app_install('system_combination')) {
            return null;
        }

        $data = DB::select('a.product_id')
            ->from(array('combination_product', 'a'))
            ->join(array('combination', 'b'), 'left')
            ->on('a.combination_id', '=', 'b.id')
            ->where('a.typeid', '=', $type_id)
            ->where('b.ishidden', '=', 0)
            ->where('a.ishidden', '=', 0)
            ->execute()
            ->as_array();

        if (empty($data)) {
            return null;
        }

        self::$product_ids = array_column($data, 'product_id');

        if (self::$instance == null) {
            self::$instance = new Model_Api_V2_Marketing_Strategy_Combination($type_id);
        }

        return self::$instance;
    }

    public function is_support($product_id)
    {
        $product_ids = self::$product_ids;
        if (!in_array($product_id, $product_ids)) {
            return false;
        }

        return array(
            'code' => $this->application_code,
            'name' => $this->application_name
        );
    }
}